import Vue from "vue";
import Vuex from "vuex";

import getters from "./getters";
import mutations from "./mutations";
import actions from "./actions";
import state from "./state";
Vue.use(Vuex);

// 下面代码通过动态加载store/modules下的每个模块下的vuex文件
// 这样就不用使用 import xxx from "./modules/xxxx";进行文件加载了
const modulesFiles = require.context("./modules", true, /\.js$/);
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");
  const value = modulesFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {});

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  modules,
});
